
local meta = FindMetaTable( "Player" );

local types = {

	"Heavy",
	"Light",

};

for k, v in pairs( types ) do
	
	meta["Set" .. v .. "Weapon"] = function( self, itemdata )
	
		self:GetTable()[v .. "Weapon"] =
		{
			
			Data = itemdata
		
		}
		
		if( k == 1 ) then
		
			umsg.Start( "HWEAP", self );
				umsg.String( itemdata.ID );
			umsg.End();			
		
		else
		
			umsg.Start( "LWEAP", self );
				umsg.String( itemdata.ID );
			umsg.End();
			
		end
		
		self:SendItemData( itemdata );
		
		self:Give( itemdata.ID );
		
		local weap = self:GetWeapon( itemdata.ID );
	
		timer.Simple( .1, self["Set" .. v .. "WeaponAmmo"], self, itemdata.AmmoCurrentClip );
	
	end
	
	meta["Drop" .. v .. "Weapon"] = function( self )
	
		if( self:GetTable()[v .. "Weapon"] ) then
		
			local item = self:DropItemProp( self:GetTable()[v .. "Weapon"].Data );	
			local weap = self:GetWeapon( self:GetTable()[ v .. "Weapon"].Data.ID );
			
			if( weap and weap:IsValid() ) then
			
				item:GetTable().ItemData.AmmoCurrentClip = weap:GetTable().Primary.CurrentClip;
			
				self:StripWeapon( self:GetTable()[v .. "Weapon"].Data.ID );
			
			end
			
			self["Remove" .. v .. "Weapon"]( self );
		
		end
	
	end
	
	meta["Set" .. v .. "WeaponAmmo"] = function( self, amt )
	
		self:GetWeapon( self:GetTable()[v .. "Weapon"].Data.ID ):GetTable().Primary.CurrentClip = amt;
		
		umsg.Start( "SAMM" );
			umsg.String( self:GetTable()[v .."Weapon"].Data.ID );
			umsg.Short( amt );
		umsg.End();
	
	end
	
	meta["TakeAmmoTo" .. v .. "Weapon"] = function( self, inv, x, y )
	
		if( self:GetTable()[v .. "Weapon"] and self:ValidInventoryItem( inv, x, y ) ) then
		
			local itemdata = self:GetTable().InventoryGrid[inv][x][y].ItemData;
		
			if( not itemdata ) then
			
				return;
			
			end		
		
	
			if( itemdata.AmmoType == self:GetTable()[v .. "Weapon"].Data.WeaponData.Primary.AmmoType ) then
			
				local amt = math.Clamp( self:GetTable()[v .. "Weapon"].Data.WeaponData.Primary.MaxAmmoClip - self:GetWeapon( self:GetTable()[v .. "Weapon"].Data.ID ):GetTable().Primary.CurrentClip, 0, self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount );
			
				local reloadcb = function()
	
					if( not self:GetTable().InventoryGrid[inv][x][y].ItemData ) then return; end
			
					self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount = self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount - amt;
				
					umsg.Start( "CIIA", self );
						umsg.Short( inv );
						umsg.Short( x );
						umsg.Short( y );
						umsg.Short( self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount );
					umsg.End();
					
					self["Set" .. v .. "WeaponAmmo"]( self, self:GetWeapon( self:GetTable()[v .. "Weapon"].Data.ID ):GetTable().Primary.CurrentClip + amt );
				
					if( self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount <= 0 ) then
					
						self:GetTable().InventoryGrid[inv][x][y].ItemData.Amount = 0;
					
						self:RemoveFromInventory( inv, x, y );
					
					end
					
				end
				
				local weap = self:GetWeapon( self:GetTable()[v .. "Weapon"].Data.ID );
				
				if( self:GetActiveWeapon():IsValid() and self:GetActiveWeapon():GetClass() == self:GetTable()[v .. "Weapon"].Data.ID ) then
				
					weap:GetTable().Primary.ReloadingAmount = amt;
					weap:GetTable().ReloadCB = reloadcb;
					weap:OnReload();
					
				else
				
					reloadcb();
				
				end
					
			end
		
		end	
	
	end
	
	meta["SetItemTo" .. v .. "Weapon"] = function( self, inv, x, y )
	
		if( self:ValidInventoryItem( inv, x, y ) ) then
		
			local itemdata = self:GetTable().InventoryGrid[inv][x][y].ItemData;
			
			if( not itemdata ) then
			
				return;
			
			end
			
			if( itemdata[v .. "Weight"] ) then
				
				self["Drop" .. v .. "Weapon"]( self );
				self:RemoveFromInventory( inv, x, y );
				self["Set" .. v .. "Weapon"]( self, itemdata ); 
			
			end
			
		end
	
	end
	
	meta["Remove" .. v .. "Weapon"] = function( self )
	
		if( k == 1 ) then
			self:CallEvent( "RMHWEAP" );
		else
			self:CallEvent( "RMLWEAP" );
		end
			
		self:GetTable()[v .. "Weapon"] = nil;
	
	end
	
	meta["Move" .. v .. "WeaponToInventory"] = function( self, inv, x, y )
	
		local itemdata = self:GetTable()[v .. "Weapon"].Data;
				
		if( self:AttemptToPutInInventoryAt( itemdata, inv, x, y ) ) then
	
			local _x = self:GetTable().InventoryGrid[inv][x][y].sx;
			local _y = self:GetTable().InventoryGrid[inv][x][y].sy;
			
			local weap = self:GetWeapon( self:GetTable()[v .. "Weapon"].Data.ID );
			
			if( self:GetTable().InventoryGrid[inv][_x] and self:GetTable().InventoryGrid[inv][_x][_y] ) then
			
				self:GetTable().InventoryGrid[inv][_x][_y].ItemData.AmmoCurrentClip = weap:GetTable().Primary.CurrentClip;		
	
			end
	
			self:StripWeapon( self:GetTable()[v .. "Weapon"].Data.ID );
			self["Remove" .. v .. "Weapon"]( self );
	
		end	
	
	end
	
end

function meta:CarryWeapon( ent )

	if( ent:GetTable().ItemData.IsWeapon ) then
	
		local type = "Light";
	
		if( ent:GetTable().ItemData.WeaponData.HeavyWeight ) then
		
			type = "Heavy";

		end
		
		self["Drop" .. type .. "Weapon"]( self );
		self["Set" .. type .. "Weapon"]( self, ent:GetTable().ItemData );
		ent:Remove();
	
	end

end




